<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Examples - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.application.examples.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.application.examples.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.application.theory-of-operation.html">Theory of Operation</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.application.html">Zend_Application</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.application.core-functionality.html">Funcionalidade principal</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.application.examples" class="section"><div class="info"><h1 class="title">Examples</h1></div>
    

    <p class="para">
        The Bootstrap class itself will typically be fairly minimal; often,
        it will simply be an empty stub extending the base bootstrap class:
    </p>

    <pre class="programlisting brush: php">
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
}
</pre>


    <p class="para">
        With a corresponding configuration file:
    </p>

    <pre class="programlisting brush: ini">
; APPLICATION_PATH/configs/application.ini
[production]
autoloaderNamespaces[] = &quot;My_&quot;
bootstrap.path = APPLICATION_PATH &quot;/Bootstrap.php&quot;
bootstrap.class = &quot;Bootstrap&quot;
pluginpaths.My_Bootstrap_Resource = &quot;My/Bootstrap/Resource&quot;
resources.frontController.controllerDirectory = APPLICATION_PATH &quot;/controllers&quot;

[testing : production]
[development : production]
</pre>


    <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>Autoloader namespaces</b><br /></span>
        

        <p class="para">
            Because these examples use custom code, we need to register the namespace prefixes for
            that code with our configuration; this is done with the
            <span class="property">autoloaderNamespaces</span> configuration key, which is an array.
        </p>

        <p class="para">
            Additionally, to ensure that custom plugin resources are discovered, we need to register
            a plugin prefix path with the bootstrap. This is done with the
            <span class="property">pluginpaths</span> configuration key, which is an associative array, with
            keys denoting the prefix to use, and values denoting the path related to that prefix.
        </p>
    </p></blockquote>

    <p class="para">
        However, should custom initialization be necessary, you have two
        choices. First, you can write methods prefixed with <em class="emphasis">_init</em>
        to specify discrete code to bootstrap. These methods will be called by
         <span class="methodname">bootstrap()</span>, and can also be called as if they were public methods:
        <em class="emphasis">bootstrap&lt;resource&gt;()</em>. They should accept an optional
        array of options.
    </p>

    <p class="para">
        If your resource method returns a value, it will be stored in a
        container in the bootstrap. This can be useful when different resources
        need to interact (such as one resource injecting itself into another).
        The method  <span class="methodname">getResource()</span> can then be used to retrieve those
        values.
    </p>

    <p class="para">
        The example below shows a resource method for initializing the request
        object. It makes use of dependency tracking (it depends on the front
        controller resource), fetching a resource from the bootstrap, and
        returning a value to store in the bootstrap.
    </p>

    <pre class="programlisting brush: php">
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initRequest()
    {
        // Ensure front controller instance is present, and fetch it
        $this-&gt;bootstrap(&#039;FrontController&#039;);
        $front = $this-&gt;getResource(&#039;FrontController&#039;);

        // Initialize the request object
        $request = new Zend_Controller_Request_Http();
        $request-&gt;setBaseUrl(&#039;/foo&#039;);

        // Add it to the front controller
        $front-&gt;setRequest($request);

        // Bootstrap will store this value in the &#039;request&#039; key of its container
        return $request;
    }
}
</pre>


    <p class="para">
        Note in this example the call to  <span class="methodname">bootstrap()</span>;
        this ensures that the front controller has been initialized prior to
        calling this method. That call may trigger either a resource or another
        method in the class.
    </p>

    <p class="para">
        The other option is to use resource plugins. Resource plugins are
        objects that perform specific initializations, and may be specified:
    </p>

    <ul class="itemizedlist">
        <li class="listitem">
            <p class="para">
                When instantiating the <span class="classname">Zend_Application</span> object
            </p>
        </li>

        <li class="listitem">
            <p class="para">
                During initialization of the bootstrap object
            </p>
        </li>

        <li class="listitem">
            <p class="para">
                By explicitly enabling them via method calls to the bootstrap
                object
            </p>
        </li>
    </ul>

    <p class="para">
        Resource plugins implement
        <span class="classname">Zend_Application_Resource_ResourceAbstract</span>,
        which defines simply that they allow injection of the caller and
        options, and that they have an  <span class="methodname">init()</span> method. As an
        example, a custom &quot;View&quot; bootstrap resource might look like the
        following:
    </p>

    <pre class="programlisting brush: php">
class My_Bootstrap_Resource_View
    extends Zend_Application_Resource_ResourceAbstract
{
    public function init()
    {
        $view = new Zend_View($this-&gt;getOptions());
        Zend_Dojo::enableView($view);

        $view-&gt;doctype(&#039;XHTML1_STRICT&#039;);
        $view-&gt;headTitle()-&gt;setSeparator(&#039; - &#039;)-&gt;append(&#039;My Site&#039;);
        $view-&gt;headMeta()-&gt;appendHttpEquiv(&#039;Content-Type&#039;,
                                           &#039;text/html; charset=utf-8&#039;);

        $view-&gt;dojo()-&gt;setDjConfigOption(&#039;parseOnLoad&#039;, true)
                     -&gt;setLocalPath(&#039;/js/dojo/dojo.js&#039;)
                     -&gt;registerModulePath(&#039;../spindle&#039;, &#039;spindle&#039;)
                     -&gt;addStylesheetModule(&#039;spindle.themes.spindle&#039;)
                     -&gt;requireModule(&#039;spindle.main&#039;)
                     -&gt;disable();

        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
            &#039;ViewRenderer&#039;
        );
        $viewRenderer-&gt;setView($view);

        return $view;
    }
}
</pre>


    <p class="para">
        To tell the bootstrap to use this, you would need to provide either the
        class name of the resource plugin, or a combination of a plugin loader prefix
        path and the short name of the resource plugin (e.g, &quot;view&quot;):
    </p>

    <pre class="programlisting brush: php">
$application = new Zend_Application(
    APPLICATION_ENV,
    array(
        &#039;resources&#039; =&gt; array(
            &#039;My_Bootstrap_Resource_View&#039; =&gt; array(), // full class name; OR
            &#039;view&#039; =&gt; array(),                       // short name

            &#039;FrontController&#039; =&gt; array(
                &#039;controllerDirectory&#039; =&gt; APPLICATION_PATH . &#039;/controllers&#039;,
            ),
        ),

        // For short names, define plugin paths:
        &#039;pluginPaths = array(
            &#039;My_Bootstrap_Resource&#039; =&gt; &#039;My/Bootstrap/Resource&#039;,
        )
    )
);
</pre>


    <p class="para">
        Resource plugins can call on other resources and initializers by accessing the
        parent bootstrap:
    </p>

    <pre class="programlisting brush: php">
class My_Bootstrap_Resource_Layout
    extends Zend_Application_Resource_ResourceAbstract
{
    public function init()
    {
        // ensure view is initialized...
        $this-&gt;getBootstrap()-&gt;bootstrap(&#039;view&#039;);

        // Get view object:
        $view = $this-&gt;getBootstrap()-&gt;getResource(&#039;view&#039;);

        // ...
    }
}
</pre>


    <p class="para">
        In normal usage, you would instantiate the application, bootstrap it,
        and run it:
    </p>

    <pre class="programlisting brush: php">
$application = new Zend_Application(...);
$application-&gt;bootstrap()
            -&gt;run();
</pre>


    <p class="para">
        For a custom script, you might need to simply initialize specific
        resources:
    </p>

    <pre class="programlisting brush: php">
$application = new Zend_Application(...);
$application-&gt;getBootstrap()-&gt;bootstrap(&#039;db&#039;);

$service = new Zend_XmlRpc_Server();
$service-&gt;setClass(&#039;Foo&#039;);  // uses database...
echo $service-&gt;handle();
</pre>


    <p class="para">
        Instead of using the  <span class="methodname">bootstrap()</span> method to call the
        internal methods or resources, you may also use overloading:
    </p>

    <pre class="programlisting brush: php">
$application = new Zend_Application(...);
$application-&gt;getBootstrap()-&gt;bootstrapDb();
</pre>

</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.application.theory-of-operation.html">Theory of Operation</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.application.html">Zend_Application</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.application.core-functionality.html">Funcionalidade principal</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="reference.html">Refer&ecirc;ncia do Zend Framework</a></li>
  <li class="header up"><a href="zend.application.html">Zend_Application</a></li>
  <li><a href="zend.application.introduction.html">Introdu&ccedil;&atilde;o</a></li>
  <li><a href="zend.application.quick-start.html">Zend_Application Quick Start</a></li>
  <li><a href="zend.application.theory-of-operation.html">Theory of Operation</a></li>
  <li class="active"><a href="zend.application.examples.html">Examples</a></li>
  <li><a href="zend.application.core-functionality.html">Funcionalidade principal</a></li>
  <li><a href="zend.application.available-resources.html">Available Resource Plugins</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>